package de.carcophan.jimdb.gwt.client.presenter;

import java.util.Arrays;
import java.util.List;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;

import de.carcophan.jimdb.gwt.client.ClientFactory;
import de.carcophan.jimdb.gwt.shared.entites.UserMovies;

public class MovieGridPresenterImpl implements MovieGridPresenter {

	private ClientFactory clientFactory;
	private Display display;

	public MovieGridPresenterImpl(ClientFactory clientFactory, Display display) {
		this.clientFactory = clientFactory;
		this.display = display;
	}

	@Override
	public void go(HasWidgets panel) {
		clientFactory.getWidgetUtil().showAjaxLoader(true);
		clientFactory.getMovieService().loadMovies(new AsyncCallback<List<UserMovies>>() {

			@Override
			public void onFailure(Throwable t) {
				clientFactory.getWidgetUtil().showAjaxLoader(false);
				Window.alert(Arrays.toString(t.getStackTrace()));
			}

			@Override
			public void onSuccess(List<UserMovies> userMovies) {
				clientFactory.getWidgetUtil().showAjaxLoader(false);
				display.setMovies(userMovies);
			}
		});

	}

	public interface Display {

		void setMovies(List<UserMovies> userMovies);

	}

}
